package jplat.core.session;

import jplat.base.constant.KPlatResponseCode;
import jplat.core.session.redis.JRedisSession;
import jplat.core.trans.JAppContext;
import jplat.error.exception.JSystemException;

import org.apache.log4j.Logger;

public class JSessionFactory
{
	private static Logger logger = Logger.getLogger(JSessionFactory.class);
	
	/**
	 * 新建一个会话.
	 * @author zhangcq
	 * @date Dec 22, 2016
	 * @comment 
	 * @param appCtx
	 * @param type
	 * @return
	 * @throws JSystemException
	 */
	public static JSession createJSession( JAppContext appCtx, int type, boolean create ) throws JSystemException
	{
		logger.info("try__SESSION_GET:force-create="+create+",type="+type);

		switch( type )
		{
		case JSession.SESS_HTTP:
			return new JHttpSession(appCtx.getRequest(),create);
		case JSession.SESS_REDIS:
			if ( create )
				return new JRedisSession();
			else
				return new JRedisSession( appCtx.getReqInfo().getStoken() );
		default:
			appCtx.failSystemException(KPlatResponseCode.CD_CONF_ERROR, KPlatResponseCode.MSG_CONF_ERROR);
		}
		
		return null;
	}
	
	/**
	 * 
	 * @Title: cleanSession   
	 * @Description: TODO(清除会话)   
	 * @return: boolean      
	 * @throws
	 */
	public static boolean cleanSession( JAppContext appCtx) throws JSystemException {
		JRedisSession redisSesson = new JRedisSession( appCtx.getReqInfo().getStoken() );
		long res = redisSesson.cleanSession();
		if(res!=-1) {
			return true ;
		}else {
			return false ;
		}
	}
	
}
